import {
    graphql,
    GraphQLSchema,
    GraphQLObjectType,
    GraphQLString,
    GraphQLList,
    GraphQLNonNull,
    GraphQLBoolean,
    GraphQLInt

} from 'graphql';
import Sequelize from 'sequelize'

const Op = Sequelize.Op

import User from '../../db/models/user'
import User_role_temp from '../../db/models/user_role_temp'
export let userType = new GraphQLObjectType({
    name: 'user',
    fields: {
        id: {
            type: GraphQLInt
        },
        user_name: {
            type: GraphQLString
        },
        user_code: {
            type: GraphQLString
        },
        user_pass: {
            type: GraphQLString
        },
        user_phone: {
            type: GraphQLString
        },
        user_office_phone: {
            type: GraphQLString
        },
        user_sex: {
            type: GraphQLInt
        },
        user_lastdate: {
            type: GraphQLString
        },
        user_nowdate: {
            type: GraphQLString
        },
        user_state: {
            type: GraphQLInt
        },
        department: {
            type: GraphQLString
        },
        company: {
            type: GraphQLString
        }
    }
})

export const userList = {
    type: new GraphQLList(userType),
    args: {
        department: {
            type: GraphQLString
        },
        company: {
            type: GraphQLString
        },
    },
    resolve(root, params, options) {
        let where = {
            company: params.company
        }
        if (params.department) {
            where.department = params.department
        }
        return User.findAll({
            where: where,
            order: ["company", "department", "user_name"]
        })
    }
}

export const userLogin = {
    type: userType,
    args: {
        user_code: {
            type: GraphQLString
        },
        user_pass: {
            type: GraphQLString
        },
    },
    resolve(root, params, options) {
        return User.findOne({
            where: {
                user_code: params.user_code.toUpperCase(),
                user_pass: params.user_pass
            }
        })
    }
}

export const userOne = {
    type: userType,
    args: {
        id: {
            type: GraphQLInt
        }
    },
    resolve(root, params, options) {
        return User.findOne({
            where: {
                id: params.id
            }
        })
    }
}
export const userUpdate = {
    type: GraphQLInt,
    args: {
        id: {
            type: GraphQLInt
        },
        user_phone: {
            type: GraphQLString
        },
        user_pass: {
            type: GraphQLString
        },
        user_office_phone: {
            type: GraphQLString
        },
        department: {
            type: GraphQLString
        },
        company: {
            type: GraphQLString
        }
    },
    resolve(root, params, options) {
        return User.update({
            user_phone: params.user_phone,
            user_office_phone: params.user_office_phone,
            department: params.department,
            company: params.company,
            user_pass: params.user_pass
        }, {
            where: {
                id: params.id
            }
        })
    }
}

export const userAdd = {
    type: userType,
    args: {
        user_name: {
            type: GraphQLString
        },
        user_code: {
            type: GraphQLString
        },
        user_pass: {
            type: GraphQLString
        },
        user_phone: {
            type: GraphQLString
        },
        user_office_phone: {
            type: GraphQLString
        },
        user_sex: {
            type: GraphQLInt
        },
        department: {
            type: GraphQLString
        },
        company: {
            type: GraphQLString
        }
    },
    resolve(root, params, options) {
        return User.create({
            user_name: params.user_name,
            user_code: params.user_code,
            user_pass: params.user_pass,
            user_phone: params.user_phone,
            user_office_phone: params.user_office_phone,
            user_sex: params.user_sex,
            user_state: 0,
            user_nowdate: new Date().toLocaleString(),
            department: params.department,
            company: params.company,
        })
    }
}

export const userDel = {
    type: GraphQLInt,
    args: {
        id: {
            type: GraphQLInt
        }
    },
    resolve(root, params, options) {
        User_role_temp.destroy({
            where: {
                user_id: params.id
            }
        })
        return User.destroy({
            where: {
                id: params.id
            }
        })
    }
}

export const userListSearch = {
    type: new GraphQLList(userType),
    args: {
        key: {
            type: GraphQLString
        },
        company: {
            type: GraphQLString
        }
    },
    resolve(root, params, options) {
        let where = {
            company: params.company,
            [Op.or]: [{
                    company: {
                        [Op.like]: '%' + params.key + '%'
                    }
                },
                {
                    department: {
                        [Op.like]: '%' + params.key + '%'
                    }
                },
                {
                    user_name: {
                        [Op.like]: '%' + params.key + '%'
                    }
                },
                {
                    user_code: {
                        [Op.like]: '%' + params.key + '%'
                    }
                },
                {
                    user_phone: {
                        [Op.like]: '%' + params.key + '%'
                    }
                },
                {
                    user_office_phone: {
                        [Op.like]: '%' + params.key + '%'
                    }
                },
                {
                    user_state: {
                        [Op.like]: '%' + params.key + '%'
                    }
                },
                {
                    user_nowdate: {
                        [Op.like]: '%' + params.key + '%'
                    }
                },
                {
                    user_lastdate: {
                        [Op.like]: '%' + params.key + '%'
                    }
                }
            ]
        }

        return User.findAll({
            where: where,
            order: ["department", "user_name"]
        })
    }
}
